package org.iteam.bank.signed.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.iteam.bank.mina.utils.NumberUtils;


public class P2PProject implements Serializable {

	/**
	 * 月标
	 */
	public static final int SCALE_TYPE_MONTH = 0;
	/**
	 * 天标
	 */
	public static final int SCALE_TYPE_DAY = 1;
	/**
	 * 年标
	 */
	public static final int SCALE_TYPE_YEAR = 2;
	/**
	 * 
	 */
	private static final long serialVersionUID = -103631794446433600L;
	private String id;
	/**
	 * 投资项目名称
	 */
	private String projectName;
	/**
	 * 投资项目详情
	 */
	private String projectContent;
	/**
	 * 投资项目借款总金额
	 */
	private BigDecimal projectAmount;
	/**
	 * 最小满标金额
	 */
	private BigDecimal projectAmountMin;
	/**
	 * 投资项目借款合同最小金额
	 */
	private BigDecimal projectMinAmount;
	/**
	 * 已投金额
	 */
	private BigDecimal castAmount = new BigDecimal(0);
	/**
	 * 利率，年化率
	 */
	private BigDecimal rate;
	/**
	 * 借款期限类别 年、月、日、
	 */
	private Integer periodType;
	/**
	 * 借款期限
	 */
	private Integer period;
	/**
	 * 是否定向标 	 	0  非定向标   1  定向标
	 */
	private Integer direct;
	/**
	 * 定向标密码
	 */
	private String directPassword;
	/**
	 * 是否必须倍投
	 */
	private Integer doubleYes;
	/**
	 * 标最小投资金额
	 */
	private BigDecimal tenderAccountMin;
	/**
	 * 标最大投资金额
	 */
	private BigDecimal tenderAccountMax;
	/**
	 * 有效期
	 */
	private Integer validTime;
	/**
	 * 合同生成时间
	 */
	private Long contractCreateTime;

	/**
	 * 合同生效时间
	 */
	private Long contractEffectiveTime;
	/**
	 * 合同到期日(失效时间)
	 */
	private Long contractExpireTime;

	/**
	 * 计息开始时间
	 */
	private Long interestStartTime;

	/**
	 * 提交时间（记录提交 草稿箱项目去审核时间）
	 */
	private Long submitTime;
	/**
	 * 提交用户（记录提交 草稿箱项目去审核人）
	 */
	private String submitUser;
	/**
	 * 上架时间
	 */
	private Long groundTime;
	/**
	 * 开标时间
	 */
	private Long tenderStart;
	/**
	 * 满标时间
	 */
	private Long fullScaleTime;
	/**
	 * 投标截止时间
	 */
	private Long tenderEnd;
	/**
	 * 项目编号
	 */
	private String projectCode;
	/**
	 * 项目合同
	 */
	private String contract;
	/**
	 * 担保函编号
	 */
	private String backletterId;
	/**
	 * 第三方担保公司
	 */
	private String guarantee;
	/**
	 * 第三方担保公司银行卡户名
	 */
	private String guaranteeBankAccount;
	/**
	 * 第三方担保公司银行卡卡号
	 */
	private String guaranteeBankCard;

	/**
	 * 渠道银行户名
	 */
	private String channelBankAccount;
	/**
	 * 渠道银行卡号
	 */
	private String channelBankCard;

	/**
	 * 具体保障措施
	 */
	private String safeguardComment;
	/**
	 * 是否有奖励 0 不奖励 1 奖励
	 */
	private Integer award = 0;
	/**
	 * 平台等级 5：A++: 4：A+: 3：A: 2：B++: 1：B+
	 */
	private Integer platformLevel;
	/**
	 * 企业情况
	 */
	private String enterpriseCase;
	/**
	 * 资金用途
	 */
	private String fundsUse;

	/**
	 * 文创汇独立意见
	 */
	private String cfhAdvice;

	/**
	 * 担保机构意见
	 */
	private String geAgcyAdvice;
	/**
	 * 奖励固定金额
	 */
	private BigDecimal awardAmount;
	/**
	 * 奖励比例
	 */
	private BigDecimal awardPercent;
	/**
	 * 流标是否奖励
	 */
	private Integer awardFail;
	/**
	 * 奖励总额
	 */
	private BigDecimal awardAmountTotal;
	/**
	 * 项目状态 @see P2pProjectDto
	 */
	private Integer status;
	/**
	 * 是否支持转让
	 */
	private Integer transferYes;
	/**
	 * 最短持有时间（天）才可以转让
	 */
	private Integer holdDay;
	/**
	 * 项目基础图片
	 */
	private String attachment;
	/**
	 * 项目头图
	 */
	private String headAttachmentId;

	/**
	 * 项目现场图片
	 */
	private String localAttachmentId;
	/**
	 * 抵押担保图片
	 */
	private String guaAttachmentId;

	/**
	 * 产品编号（用于银行接口）
	 */
	private String productNo;
	/**
	 * 归集帐号（用于对接银行接口相关操作,主要用于放款）
	 */
	private String ecardNo;
	/**
	 * 归集帐号名称（用于对接银行接口相关操作,主要用于放款）
	 */
	private String ecardName;
	/**
	 * 归集余额
	 */
	private BigDecimal ecardAmount;

	/**
	 * 融资人
	 */
	private String p2pUser_id;
	/**
	 * 招标类型
	 */
	private String p2pEnumByTenderType;
	/**
	 * 还款方式
	 */
	private String p2pEnumByRepayType_id;
	/**
	 * 是否推荐项目 1为推荐，0或空为不推荐
	 */
	private Integer isRecommendProject;
	/**
	 * 借款用途
	 */
	private String p2pEnumByBorrowUse;
	/**
	 * 项目自定义分类 从p2p_enum.type="PROJECT_CUSTOMTAG" 获取
	 */
	private String p2pEnumByCustomTag;
	/**
	 * 放款提交状态：用于把放款的数据调用银行的接口提交给银行的状态保存. 0: 未提交 ：界面显示连接：提交数据 1: 已提交
	 * ：界面显示文字：数据已提交 2: 提交失败:前台显示.数据提交失败 重新提交
	 */
	private Integer loanSubmitState = 0;

	// p2pEnumByTenderType_id
	// 标种名称ID
	/**
	 * 10090 0 0 0 抵押标 P2P_TENDER_NAME 10091 0 0 1 担保标 P2P_TENDER_NAME
	 */
	private String p2pEnumByTenderType_id;
	private String p2pProjectExtend_id;

	public String getNumberSString(BigDecimal decimal) {
		return NumberUtils.getNumberSString(decimal);
	}

	public String getNumberS2String(BigDecimal decimal) {
		String value = NumberUtils.getNumberSString(decimal);
		if (value.endsWith(".00")) {
			return value.substring(0, value.length() - 3);
		}
		return value;
	}

	public String getNumberWString(BigDecimal decimal) {
		return NumberUtils.getNumberWString(decimal);
	}

	public String getP2pEnumByTenderType_id() {
		return p2pEnumByTenderType_id;
	}

	public void setP2pEnumByTenderType_id(String p2pEnumByTenderType_id) {
		this.p2pEnumByTenderType_id = p2pEnumByTenderType_id;
	}

	public String getP2pProjectExtend_id() {
		return p2pProjectExtend_id;
	}

	public void setP2pProjectExtend_id(String p2pProjectExtend_id) {
		this.p2pProjectExtend_id = p2pProjectExtend_id;
	}

	/**
	 * 合同生成时间:秒
	 */
	public Long getContractCreateTime() {
		return contractCreateTime;
	}

	/**
	 * 合同生成时间:秒
	 */
	public void setContractCreateTime(Long contractCreateTime) {
		this.contractCreateTime = contractCreateTime;
	}

	/**
	 * 合同生效时间:秒
	 */
	public Long getContractEffectiveTime() {
		return contractEffectiveTime;
	}

	/**
	 * 合同生效时间:秒
	 */
	public void setContractEffectiveTime(Long contractEffectiveTime) {
		this.contractEffectiveTime = contractEffectiveTime;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getProjectContentBr() {
		try {
			StringBuffer content = new StringBuffer(projectContent);
			int i = 2;
			int index = -1;
			while ((index = content.indexOf(String.valueOf(i++).concat("、"), index + 3)) > 0) {
				content.insert(index, "<br/>");
			}
			return content.toString();
		} catch (Exception e) {

		}
		return projectContent;
	}

	public String getProjectContent() {
		return projectContent;
	}

	public void setProjectContent(String projectContent) {
		this.projectContent = projectContent;
	}

	public BigDecimal getProjectAmount() {
		return projectAmount;
	}

	public void setProjectAmount(BigDecimal projectAmount) {
		this.projectAmount = projectAmount;
	}

	public BigDecimal getProjectAmountMin() {
		return projectAmountMin;
	}

	public void setProjectAmountMin(BigDecimal projectAmountMin) {
		this.projectAmountMin = projectAmountMin;
	}

	public BigDecimal getProjectMinAmount() {
		return projectMinAmount;
	}

	public void setProjectMinAmount(BigDecimal projectMinAmount) {
		this.projectMinAmount = projectMinAmount;
	}

	public BigDecimal getCastAmount() {
		return castAmount;
	}

	public void setCastAmount(BigDecimal castAmount) {
		this.castAmount = castAmount;
	}

	public BigDecimal getRate() {
		return rate;
	}

	public void setRate(BigDecimal rate) {
		this.rate = rate;
	}

	public Integer getDirect() {
		return direct;
	}

	public void setDirect(Integer direct) {
		this.direct = direct;
	}

	public String getDirectPassword() {
		return directPassword;
	}

	public void setDirectPassword(String directPassword) {
		this.directPassword = directPassword;
	}

	public Integer getDoubleYes() {
		return doubleYes;
	}

	public void setDoubleYes(Integer doubleYes) {
		this.doubleYes = doubleYes;
	}

	public BigDecimal getTenderAccountMin() {
		return tenderAccountMin;
	}

	public void setTenderAccountMin(BigDecimal tenderAccountMin) {
		this.tenderAccountMin = tenderAccountMin;
	}

	public BigDecimal getTenderAccountMax() {
		return tenderAccountMax;
	}

	public void setTenderAccountMax(BigDecimal tenderAccountMax) {
		this.tenderAccountMax = tenderAccountMax;
	}

	public Integer getValidTime() {
		return validTime;
	}

	public void setValidTime(Integer validTime) {
		this.validTime = validTime;
	}

	public Long getContractExpireTime() {
		return contractExpireTime;
	}

	public void setContractExpireTime(Long contractExpireTime) {
		this.contractExpireTime = contractExpireTime;
	}

	public Long getInterestStartTime() {
		return interestStartTime;
	}

	public void setInterestStartTime(Long interestStartTime) {
		this.interestStartTime = interestStartTime;
	}

	public Long getSubmitTime() {
		return submitTime;
	}

	public void setSubmitTime(Long submitTime) {
		this.submitTime = submitTime;
	}

	public String getSubmitUser() {
		return submitUser;
	}

	public void setSubmitUser(String submitUser) {
		this.submitUser = submitUser;
	}

	public Long getGroundTime() {
		return groundTime;
	}

	public void setGroundTime(Long groundTime) {
		this.groundTime = groundTime;
	}

	public Long getTenderStart() {
		return tenderStart;
	}

	public void setTenderStart(Long tenderStart) {
		this.tenderStart = tenderStart;
	}

	public Long getFullScaleTime() {
		return fullScaleTime;
	}

	public void setFullScaleTime(Long fullScaleTime) {
		this.fullScaleTime = fullScaleTime;
	}

	public Long getTenderEnd() {
		return tenderEnd;
	}

	public void setTenderEnd(Long tenderEnd) {
		this.tenderEnd = tenderEnd;
	}

	public String getProjectCode() {
		return projectCode;
	}

	public void setProjectCode(String projectCode) {
		this.projectCode = projectCode;
	}

	public String getContract() {
		return contract;
	}

	public void setContract(String contract) {
		this.contract = contract;
	}

	public String getBackletterId() {
		return backletterId;
	}

	public void setBackletterId(String backletterId) {
		this.backletterId = backletterId;
	}

	public String getGuarantee() {
		return guarantee;
	}

	public void setGuarantee(String guarantee) {
		this.guarantee = guarantee;
	}

	public String getGuaranteeBankAccount() {
		return guaranteeBankAccount;
	}

	public void setGuaranteeBankAccount(String guaranteeBankAccount) {
		this.guaranteeBankAccount = guaranteeBankAccount;
	}

	public String getGuaranteeBankCard() {
		return guaranteeBankCard;
	}

	public void setGuaranteeBankCard(String guaranteeBankCard) {
		this.guaranteeBankCard = guaranteeBankCard;
	}

	public String getChannelBankAccount() {
		return channelBankAccount;
	}

	public void setChannelBankAccount(String channelBankAccount) {
		this.channelBankAccount = channelBankAccount;
	}

	public String getChannelBankCard() {
		return channelBankCard;
	}

	public void setChannelBankCard(String channelBankCard) {
		this.channelBankCard = channelBankCard;
	}

	public String getSafeguardComment() {
		return safeguardComment;
	}

	public void setSafeguardComment(String safeguardComment) {
		this.safeguardComment = safeguardComment;
	}

	public Integer getAward() {
		return award;
	}

	public void setAward(Integer award) {
		this.award = award;
	}

	public Integer getPlatformLevel() {
		return platformLevel;
	}

	public void setPlatformLevel(Integer platformLevel) {
		this.platformLevel = platformLevel;
	}

	public String getEnterpriseCase() {
		return enterpriseCase;
	}

	public void setEnterpriseCase(String enterpriseCase) {
		this.enterpriseCase = enterpriseCase;
	}

	public String getFundsUse() {
		return fundsUse;
	}

	public void setFundsUse(String fundsUse) {
		this.fundsUse = fundsUse;
	}

	public String getCfhAdvice() {
		return cfhAdvice;
	}

	public void setCfhAdvice(String cfhAdvice) {
		this.cfhAdvice = cfhAdvice;
	}

	public String getGeAgcyAdvice() {
		return geAgcyAdvice;
	}

	public void setGeAgcyAdvice(String geAgcyAdvice) {
		this.geAgcyAdvice = geAgcyAdvice;
	}

	public BigDecimal getAwardAmount() {
		return awardAmount;
	}

	public void setAwardAmount(BigDecimal awardAmount) {
		this.awardAmount = awardAmount;
	}

	public BigDecimal getAwardPercent() {
		return awardPercent;
	}

	public void setAwardPercent(BigDecimal awardPercent) {
		this.awardPercent = awardPercent;
	}

	public Integer getAwardFail() {
		return awardFail;
	}

	public void setAwardFail(Integer awardFail) {
		this.awardFail = awardFail;
	}

	public BigDecimal getAwardAmountTotal() {
		return awardAmountTotal;
	}

	public void setAwardAmountTotal(BigDecimal awardAmountTotal) {
		this.awardAmountTotal = awardAmountTotal;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getTransferYes() {
		return transferYes;
	}

	public void setTransferYes(Integer transferYes) {
		this.transferYes = transferYes;
	}

	public Integer getHoldDay() {
		return holdDay;
	}

	public void setHoldDay(Integer holdDay) {
		this.holdDay = holdDay;
	}

	public String getAttachment() {
		return attachment;
	}

	public void setAttachment(String attachment) {
		this.attachment = attachment;
	}

	public String getHeadAttachmentId() {
		return headAttachmentId;
	}

	public void setHeadAttachmentId(String headAttachmentId) {
		this.headAttachmentId = headAttachmentId;
	}

	public String getLocalAttachmentId() {
		return localAttachmentId;
	}

	public void setLocalAttachmentId(String localAttachmentId) {
		this.localAttachmentId = localAttachmentId;
	}

	public String getGuaAttachmentId() {
		return guaAttachmentId;
	}

	public void setGuaAttachmentId(String guaAttachmentId) {
		this.guaAttachmentId = guaAttachmentId;
	}

	public String getProductNo() {
		return productNo;
	}

	public void setProductNo(String productNo) {
		this.productNo = productNo;
	}

	public String getEcardNo() {
		return ecardNo;
	}

	public void setEcardNo(String ecardNo) {
		this.ecardNo = ecardNo;
	}

	public String getEcardName() {
		return ecardName;
	}

	public void setEcardName(String ecardName) {
		this.ecardName = ecardName;
	}

	public BigDecimal getEcardAmount() {
		return ecardAmount;
	}

	public void setEcardAmount(BigDecimal ecardAmount) {
		this.ecardAmount = ecardAmount;
	}

	public String getP2pUser_id() {
		return p2pUser_id;
	}

	public void setP2pUser_id(String p2pUser_id) {
		this.p2pUser_id = p2pUser_id;
	}

	public String getP2pEnumByTenderType() {
		return p2pEnumByTenderType;
	}

	public void setP2pEnumByTenderType(String p2pEnumByTenderType) {
		this.p2pEnumByTenderType = p2pEnumByTenderType;
	}

	public Integer getIsRecommendProject() {
		return isRecommendProject;
	}

	public void setIsRecommendProject(Integer isRecommendProject) {
		this.isRecommendProject = isRecommendProject;
	}

	public String getP2pEnumByBorrowUse() {
		return p2pEnumByBorrowUse;
	}

	public void setP2pEnumByBorrowUse(String p2pEnumByBorrowUse) {
		this.p2pEnumByBorrowUse = p2pEnumByBorrowUse;
	}

	public String getP2pEnumByCustomTag() {
		return p2pEnumByCustomTag;
	}

	public void setP2pEnumByCustomTag(String p2pEnumByCustomTag) {
		this.p2pEnumByCustomTag = p2pEnumByCustomTag;
	}

	public Integer getLoanSubmitState() {
		return loanSubmitState;
	}

	public void setLoanSubmitState(Integer loanSubmitState) {
		this.loanSubmitState = loanSubmitState;
	}

	public static int getScaleTypeMonth() {
		return SCALE_TYPE_MONTH;
	}

	public void setPeriod(Integer period) {
		this.period = period;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getP2pEnumByRepayType_id() {
		return p2pEnumByRepayType_id;
	}

	public void setP2pEnumByRepayType_id(String p2pEnumByRepayType_id) {
		this.p2pEnumByRepayType_id = p2pEnumByRepayType_id;
	}

	public void setPeriodType(Integer periodType) {
		this.periodType = periodType;
	}

	public Integer getPeriod() {
		return period;
	}

	public Integer getPeriodType() {
		return periodType;
	}

	/**
	 * 利率，年化率.百分比
	 */
	public String getRateP() {
		BigDecimal rateP = (new BigDecimal("100")).multiply(rate).setScale(2);
		String temp = rateP.toString();
		while (temp.endsWith("0")) {
			temp = temp.substring(0, temp.length() - 1);
		}
		if (temp.endsWith(".")) {
			temp = temp.substring(0, temp.length() - 1);
		}
		return temp;
		// return (new BigDecimal("100")).multiply(rate).setScale(2);
	}

	/**
	 * 利率，年化率.百分比
	 */
	public void setRateP(BigDecimal rateP) {
		this.rate = rateP != null ? rateP.divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO;
	}

	/**
	 * 期限单位名称
	 */
	public String getPeriodTypeName() {
		if (getPeriodType() == SCALE_TYPE_DAY) {
			return "天";
		} else if (getPeriodType() == SCALE_TYPE_MONTH) {
			return "个月";
		} else if (getPeriodType() == SCALE_TYPE_YEAR) {
			return "年";
		} else {
			return "未知";
		}
	}

	/**
	 * 借款期限+期限单位
	 */
	public String getPeriodS() {
		return String.format("%d%s", period, getPeriodTypeName());
	}

	/**
	 * 预计还款日期
	 * <hr/>
	 * 用融资结束日期+融资期限 。
	 * 
	 * @return
	 */
	public String getExpectedDateOfRepayment() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(interestStartTime * 1000);
		switch (getPeriodType()) {
		case SCALE_TYPE_DAY:// 天
			calendar.add(Calendar.DAY_OF_YEAR, period);
			break;
		case SCALE_TYPE_MONTH:// 月=
			calendar.add(Calendar.MONTH, period);

			break;
		case SCALE_TYPE_YEAR:// 年
			calendar.add(Calendar.YEAR, period);
			break;

		default:
			break;
		}
		// calendar.add(Calendar.DAY_OF_YEAR, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// tenderEnd
		return sdf.format(calendar.getTime());
	}

	/**
	 * 投资金额-万元
	 * 
	 * @return
	 */
	public String getProjectAmountW() {
		return NumberUtils.getNumberWString(projectAmount);
	}

	/**
	 * 已投金额
	 * 
	 * @return
	 */
	public String getCastAmountS() {
		return NumberUtils.getNumberSString(castAmount);
	}

	/**
	 * 剩余投资
	 * 
	 * @return
	 */
	public String getLeftCastAmountS() {
		return NumberUtils.getNumberSString(projectAmount.subtract(castAmount));
	}

	/**
	 * 最小倍投金额
	 * 
	 * @return
	 */
	public String getTenderAccountMinS() {
		return String.format("%.2f", tenderAccountMin.setScale(2).doubleValue());
	}

	/**
	 * 投资进度
	 * 
	 * @return
	 */
	public BigDecimal getFinancingSchedule() {
		return (new BigDecimal("100")).multiply(castAmount.divide(projectAmount, 4, BigDecimal.ROUND_HALF_UP)).setScale(2);
	}
}
